<?php
/**
 * Pipahr
 * @brief A model to deal with notify_tpl, offer_tpl, decline_tpl
 *
 * @author Randy Zhao
 * @copyright 2013 pipahr.com
 */
class PCalendarModelTpl extends JModelLegacy
{
   /**
    * Function to get all notify or offer tpls by current userid
    * @return array notify_tpls
    */
   public function getAllTpl($tpl_type) {
      if ( empty($tpl_type) ) {
         $this->setError("网络异常，请刷新重试！");
         return false;
      }

      if ( $tpl_type == "notify" ) {
         $table_name = "*PREFIX*calendar_notify_tpl";
      } elseif ( $tpl_type == "offer" ) {
         $table_name = "*PREFIX*calendar_offer_tpl";
      } else {
         $this->setError("网络异常，请刷新重试！");
         return false;
      }

      $user = PFactory::getMasterUser();
      $db = PFactory::getDbo();
      $query = "SELECT * FROM ".$table_name." WHERE userid = ".intval($user->id);
      $db->setQuery(PUtil::replaceOCPrefix($query));
      $tpls = $db->loadAssocList();
      return $tpls;
   }

   /**
    * Function to get one notify tpl by tpl's id
    * @param int $id tpl's id
    * @param string $tpl_type tpl's type (notify or offer)
    * @return array  notify_tpl
    */
   public function getNotifyTplById($id, $tpl_type) {
      if ( empty($id) || empty($tpl_type) ) {
         $this->setError("网络异常，请刷新重试！");
         return false;
      }

      if ( $tpl_type == "notify" ) {
         $table_name = "*PREFIX*calendar_notify_tpl";
         $fileds = "*";
      } elseif ( $tpl_type == "offer" ) {
         $table_name = "*PREFIX*calendar_offer_tpl";
         $fileds = "tpl_text";
      } else {
         $this->setError("网络异常，请刷新重试！");
         return false;
      }
      
      
      $user = PFactory::getMasterUser();
      $db = PFactory::getDbo();
      $query = "SELECT ".$fileds." FROM ".$table_name." WHERE id = ".intval($id)." AND userid = ".intval($user->id);
      $db->setQuery(PUtil::replaceOCPrefix($query));
      if ( $tpl_type == "notify" ) {
         $notify_tpl = $db->loadObject();
      } elseif ( $tpl_type == "offer" ) {
         $notify_tpl = $db->loadResult();
      }

      if ( empty($notify_tpl) ) {
         $this->setError("您还没有权限获取此模板");
         return false;
      }

      return $notify_tpl;
   }

   /**
    * Add a notify tpl
    * @return int  insert id
    */
   public function addNotifyTpl() {
      $user = PFactory::getMasterUser();
      $db = PFactory::getDbo();

      $tpl_type = JRequest::getString('tpl_type');
      if ( empty($tpl_type) ) {
         $this->setError("网络异常，请刷新重试！");
         return false;
      }

      if ( $tpl_type == "notify" ) {
         $table_name = "*PREFIX*calendar_notify_tpl";
      } elseif ( $tpl_type == "offer" ) {
         $table_name = "*PREFIX*calendar_offer_tpl";
      } else {
         $this->setError("网络异常，请刷新重试！");
         return false;
      }

      // Check name and text
      $tpl_name = JRequest::getString('tplname');
      $tpl_adress = JRequest::getString('address');
      $tpl_contact_phone = JRequest::getString('contact_phone');
      $tpl_contact = JRequest::getString('contact');
      $tpl_text = JRequest::getString('tpl');
      if ( empty($tpl_name) ) {
         $this->setError("请填写模板名称！");
         return false;
      }
      if($tpl_type == 'notify'){
         $tpl_adress = JRequest::getString('address');
         $tpl_contact_phone = JRequest::getString('contact_phone');
         $tpl_contact = JRequest::getString('contact');
         if(empty($tpl_adress) ) {
             $this->setError("请填写面试地点!");
             return false;
          }
         if(empty($tpl_contact_phone) ) {
            $this->setError("请填写联系电话!");
            return false;
         }else if(!JobHelper::isPhone($tpl_contact_phone)) {
            $this->setError("请输入正确的联系电话！");
            return false;
         }
         if(!empty($tpl_contact)&&!preg_match('/^(?!\d+$)[^\n]*$/', $tpl_contact)) {
            $this->setError("联系人不能为纯数字！");
            return false;
         }
      }elseif($tpl_type == 'offer'){
         if(empty($tpl_text)){
            $this->setError("请填写补充，模板内容不能为空！");
            return false;
         }
      }
      // Check tpl num
      $query = "SELECT COUNT(*) FROM ".$table_name." WHERE userid = ".intval($user->id);
      $db->setQuery(PUtil::replaceOCPrefix($query));
      $count = $db->loadResult();
      if ( $count >= 3 ) {
         $this->setError("模板数已达上限，请先删除已存模板!");
         return false;
      }

      $query = "SELECT COUNT(*) FROM $table_name WHERE userid = " . intval($user->id) . " AND tpl_name = " . $db->quote($tpl_name);
      $db->setQuery(PUtil::replaceOCPrefix($query));
      $result = $db->loadResult();
      if(!empty($result)) {
         $this->setError("该模板名已存在,请使用不同的模板名!");
         return false;
      }

      // Insert tpl table
      if($tpl_type == 'notify'){
         $query = "INSERT INTO ".$table_name." (`userid`,`tpl_name`,`tpl_text`,`address`,`contact_phone`,`contact`) VALUES (".intval($user->id).",".$db->quote($tpl_name).",".$db->quote($tpl_text).",".$db->quote($tpl_adress).",".$db->quote($tpl_contact_phone).",".$db->quote($tpl_contact).")";
      }else{
         $query = "INSERT INTO ".$table_name." (`userid`,`tpl_name`,`tpl_text`) VALUES (".intval($user->id).",".$db->quote($tpl_name).",".$db->quote($tpl_text).")";
      }
      $db->setQuery(PUtil::replaceOCPrefix($query));
      if ( !$db->execute() ) {
         $this->setError("网络异常，请重试！");
         return false;
      } else {
         $notify_id = $db->insertid();
      }

      return $notify_id;
   }

   /**
    * Delete a notify tpl by notify id
    * @param  int  $id  tpl's id
    * @param string $tpl_type tpl's type (notify or offer)
    * @return bool
    */
   public function deleteNotifyTpl($id, $tpl_type) {
      if ( empty($id) || empty($tpl_type) ) {
         $this->setError("网络异常，请刷新重试！");
         return false;
      }

      if ( $tpl_type == "notify" ) {
         $table_name = "*PREFIX*calendar_notify_tpl";
      } elseif ( $tpl_type == "offer" ) {
         $table_name = "*PREFIX*calendar_offer_tpl";
      } else {
         $this->setError("网络异常，请刷新重试！");
         return false;
      }

      // Check owner
      $user = PFactory::getMasterUser();
      $db = PFactory::getDbo();
      $query = "SELECT COUNT(*) FROM ".$table_name." WHERE id =".intval($id)." AND userid = ".intval($user->id);
      $db->setQuery(PUtil::replaceOCPrefix($query));
      $count = $db->loadResult();
      if ( $count <= 0 ) {
         $this->setError("您还没有权限删除此模板");
         return false;
      }

      // Deletion
      $query = "DELETE FROM ".$table_name." WHERE id = ".intval($id)." AND userid = ".intval($user->id);
      $db->setQuery(PUtil::replaceOCPrefix($query));
      if ( !$db->execute() ) {
         $this->setError("网络异常，请重试！");
         return false;
      }

      return true;
   }

}
